<?php
/**
 * the layout plugin fetches the layout options which the Ui resource plugin passed to the layout instance
 *   
 *
 */
class Dig_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch (Zend_Controller_Request_Abstract $request)
    {
        $layout = Zend_Layout::getMvcInstance();
        $layoutDisabled = $request->getParam('disableLayout', false);
        if ($layoutDisabled) {
            $layout->disableLayout();
        } else {
            $options = $layout->digitalusOptions;
            // set the default templates
            $template = $options['template'];
            $skin = $options['skin'];
            $scope = $options['scope'];
            // determine if the site node has overridden them
            $params = $request->getParams();
            $navigationNode = isset($params['navigation_node']) ? $params['navigation_node'] : null;
            ;
            if ($navigationNode instanceof Dig_Navigation_Page_Abstract) {
                $templateData = $navigationNode->getTemplate();
                // set the template and the skin if they have been overridden
                $template = ($templateData['template']) ? $templateData['template'] : $template;
                $skin = ($templateData['skin']) ? $templateData['skin'] : $skin;
                $scope = ($templateData['scope']) ? $templateData['scope'] : $scope;
            }
            $skin = Dig_Ui_Skin::load($skin, $template, $scope);
            $layout->setLayoutPath($skin->layout->path);
            $layout->setLayout($skin->layout->script);
        }
    }
}